/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.graph;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.ConstantVertexStringer;
import edu.uci.ics.jung.graph.decorators.EllipseVertexShapeFunction;
import edu.uci.ics.jung.graph.decorators.PickableVertexPaintFunction;
import edu.uci.ics.jung.graph.decorators.StringLabeller;
import edu.uci.ics.jung.graph.decorators.StringLabeller.UniqueLabelException;
import edu.uci.ics.jung.visualization.DefaultGraphLabelRenderer;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.ShapePickSupport;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import java.awt.Color;
import java.util.Iterator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.openide.util.Exceptions;
import pl.tom.social.analyzer.graph.adapter.SNToolTipFunction;
import pl.tom.social.analyzer.graph.mouse.PopupMousePlugin;

/**
 *
 * @author tom
 */
public class GraphNode {

	private static final Logger logger = Logger.getLogger(GraphNode.class);
	private StringLabeller strLabeller = null;
	private GraphBuilder graphBuilder = null;
	private VisualizationViewer viewer = null;

	public GraphNode(GraphBuilder graphBuilder) {
		this.graphBuilder = graphBuilder;
		Graph g = getGraph();
		strLabeller = StringLabeller.getLabeller(g);
		for(Iterator iter = g.getVertices().iterator();iter.hasNext();) {
			try {
				Vertex v = (Vertex) iter.next();
				strLabeller.setLabel(v, (String) v.getUserDatum("url"));
			} catch(UniqueLabelException ex) {
				logger.log(Level.ERROR, null, ex);
			}
		}
	}

	public StringLabeller getStrLabeller() {
		return strLabeller;
	}

	public VisualizationViewer getViewer() {
		if(viewer == null) {
			show();
		}
		return viewer;
	}

	public Graph getGraph() {
		return graphBuilder.getGraph();
	}

	public void useToottipNames(boolean flag) {
		if(flag) {
			this.viewer.setToolTipFunction(new SNToolTipFunction(getStrLabeller()));
		} else {
			this.viewer.setToolTipFunction(null);
		}
	}

	public void useVertexNames(boolean flag) {
		if(flag) {
			((PluggableRenderer) this.viewer.getRenderer()).setVertexStringer(strLabeller);
		} else {
			((PluggableRenderer) this.viewer.getRenderer()).setVertexStringer(new ConstantVertexStringer(null));
		}
	}

	public void refresh() {
		this.viewer.repaint();
	}

	public VisualizationViewer show() {
		Layout layout = new FRLayout(getGraph());
		PluggableRenderer pr = new PluggableRenderer();
		pr.setVertexShapeFunction(new EllipseVertexShapeFunction());
		pr.setVertexPaintFunction(new PickableVertexPaintFunction(pr, Color.lightGray, Color.white, Color.yellow));
		pr.setGraphLabelRenderer(new DefaultGraphLabelRenderer(Color.cyan, Color.cyan));
		pr.setVertexStringer(strLabeller);
		VisualizationViewer vv = new VisualizationViewer(layout, pr);
		vv.setPickSupport(new ShapePickSupport());
		vv.setBackground(Color.white);
		DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
		vv.setGraphMouse(gm);
		vv.setToolTipFunction(new SNToolTipFunction(getStrLabeller()));
		vv.getModel().setRelaxerThreadSleepTime(500);
		this.viewer = vv;

		gm.add(new PopupMousePlugin());

		this.useToottipNames(false);
		this.useVertexNames(true);

		return viewer;
	}
}
